<?php

namespace App\Admin\Controllers;

use App\City;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;
use Encore\Admin\Tree;

class CityController extends Controller
{
    use HasResourceActions;

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header('城市管理')
            ->description('目前暂定一级为城市 二级为城市区域')
            ->body($this->tree());
    }
    protected function tree()
    {

        return City::tree(function (Tree $tree) {

            $tree->branch(function ($data) {
                if($data['parent_id']==0){
                    return "城市 -  {$data['title']}";
                }else {
                    return "区域 -  {$data['title']}";
                }

            });

        });
    }
    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header('详情')
            ->description('description')
            ->body($this->detail($id));
    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header('编辑城市区域')
            ->description('编辑城市区域')
            ->body($this->form()->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header('新建城市区域')
            ->description('新建城市区域')
            ->body($this->form());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
{
    $grid = new Grid(new City);

    $grid->id('Id');
    $grid->title('Title');
    $grid->parent_id('Parent id');

    $grid->order('Order');
    $grid->created_at('Created at');
    $grid->updated_at('Updated at');

    return $grid;
}

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(City::findOrFail($id));

        $show->id('Id');
        $show->title('城市区域');

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new City);

        $form->select('parent_id', '所属类别')->options(City::selectOptions());
        $form->text('title', '城市区域')->rules('required');
        $states = [
            'on'  => ['value' => 1, 'text' => '热门', 'color' => 'success'],
            'off' => ['value' => 0, 'text' => '非热门', 'color' => 'danger'],
        ];

        $form->switch('is_hot','热门')->states($states);
        return $form;
    }
}
